<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="jsunit/app/jsUnitCore.js"></script>
    <script type="text/javascript" src="../src/trial.js"></script>
    <script type="text/javascript">
    
		function setUp(){
			
		}
		
		function tearDown(){
			
		}
		
		function test_monitorFunc(){
		
			var myFunc_count = 0;
			
			var obj = {
				myFunc: function(){
					myFunc_count++;
					return myFunc_count * 10;
				}	
			};
			
			trial.monitorFunc(obj, "myFunc");
			
			assertNotUndefined("obj.callsTo", obj.callsTo);
			assertNotUndefined("obj.callsTo.myFunc", obj.callsTo.myFunc);
			assertEquals(0, obj.callsTo.myFunc.length);
			
			assertNotUndefined("obj.numberOfCallsTo", obj.numberOfCallsTo);
			assertNotUndefined("obj.numberOfCallsTo.myFunc", obj.numberOfCallsTo.myFunc);
			assertEquals(0, obj.numberOfCallsTo.myFunc);
		
			assertNotUndefined("obj.lastCallTo", obj.lastCallTo);
			assertNotUndefined("obj.lastCallTo.myFunc", obj.lastCallTo.myFunc);
			assertNull("obj.lastCallTo.myFunc", obj.lastCallTo.myFunc);
		
			// call the function
			var realResult = obj.myFunc(123, 456, 789);
			
			// ensure the original function was still called
			assertEquals("Original function was not called", 1, myFunc_count);
		
			assertEquals("numberOfCallsTo.myFunc did not get increased.", 1, obj.numberOfCallsTo.myFunc);
			assertEquals("callsTo did not get given call details.", 1, obj.callsTo.myFunc.length);
			
			// make sure the callsTo object contains the right information
			var callDetails = obj.callsTo.myFunc[0];
			
			// make sure lastCallTo also works
			assertEquals(obj.callsTo.myFunc[0], obj.lastCallTo.myFunc);
			
			assertEquals(3, callDetails.arguments.length);
			assertEquals(123, callDetails.arguments[0]);
			assertEquals(456, callDetails.arguments[1]);
			assertEquals(789, callDetails.arguments[2]);
			
			// make sure there is a null error
			assertNull(callDetails.error);
			
			// make sure the result was recorded
			assertEquals(myFunc_count * 10, callDetails.result);
			
			// make sure the actual method returned the result too
			assertEquals("Result from child function was not returned.", myFunc_count * 10, realResult);
			
		}
		
		function test_monitorFunc_with_error(){
		
			var myFunc_count = 0;
			
			var obj = {
				myFunc: function(){
					myFunc_count++;
					throw "Error!"
				}	
			};
			
			trial.monitorFunc(obj, "myFunc");
			
			assertNotUndefined("obj.callsTo", obj.callsTo);
			assertNotUndefined("obj.callsTo.myFunc", obj.callsTo.myFunc);
			assertEquals(0, obj.callsTo.myFunc.length);
			
			assertNotUndefined("obj.numberOfCallsTo", obj.numberOfCallsTo);
			assertNotUndefined("obj.numberOfCallsTo.myFunc", obj.numberOfCallsTo.myFunc);
			assertEquals(0, obj.numberOfCallsTo.myFunc);
		
			// call the function
			var realResult = obj.myFunc(123, 456, 789);
			
			// ensure the original function was still called
			assertEquals("Original function was not called", 1, myFunc_count);
		
			assertEquals("numberOfCallsTo.myFunc did not get increased.", 1, obj.numberOfCallsTo.myFunc);
			assertEquals("callsTo did not get given call details.", 1, obj.callsTo.myFunc.length);
			
			// make sure the callsTo object contains the right information
			var callDetails = obj.callsTo.myFunc[0];
			
			assertEquals(3, callDetails.arguments.length);
			assertEquals(123, callDetails.arguments[0]);
			assertEquals(456, callDetails.arguments[1]);
			assertEquals(789, callDetails.arguments[2]);
			
			// make sure there is a null error
			assertNotNull(callDetails.error);
			
			// make sure the result was recorded
			assertUndefined("callDetails.result", callDetails.result);

		}
		
		function test_monitoring_whole_object(){
		
			var functionOne_count = 0;
			var functionTwo_count = 0;
		
			var obj = {
				
				functionOne: function(){
					functionOne_count++;
				},
				
				functionTwo: function(){
					functionTwo_count++;
				}
				
			};
			
			trial.monitor(obj);
			
			assertNotUndefined("obj.callsTo", obj.callsTo);
			assertNotUndefined("obj.callsTo.functionOne", obj.callsTo.functionOne);
			assertNotUndefined("obj.callsTo.functionTwo", obj.callsTo.functionTwo);
			assertEquals(0, obj.callsTo.functionOne.length);
			assertEquals(0, obj.callsTo.functionTwo.length);
			
			assertNotUndefined("obj.numberOfCallsTo", obj.numberOfCallsTo);
			assertNotUndefined("obj.numberOfCallsTo.functionOne", obj.numberOfCallsTo.functionOne);
			assertNotUndefined("obj.numberOfCallsTo.functionTwo", obj.numberOfCallsTo.functionTwo);
			assertEquals(0, obj.numberOfCallsTo.functionOne);
			assertEquals(0, obj.numberOfCallsTo.functionTwo);
			
		}
		
    </script>
</head>
<body>
</body>
</html>